import tpl from './Luckdraw.tpl';
import './Luckdraw.scss';
import list from './list/list'

import { tplReplace } from '../../utils/tools'
import $ from 'jquery'

export default _ => {

    const Child = list();

    return {
        name: 'Luckdraw',
        tpl( obj ) {
            return tplReplace( tpl, {
                list: Child.tpl( this.handleValue(obj) )
            })
        },
        
        handleValue( obj ) {
            const _action = {
                'draw_3': _ => {
                    this.Swap( obj, 3, obj.length - 1);
                },
                'draw_4': _ => {
                    this.Swap( obj, 4, obj.length - 1);
                },
                'draw_5': _ => {
                    this.Swap( obj, 5, obj.length - 2);
                }
            }

            for( let i in _action ) {
                const branch = _action[`${i}`];
                branch.call(this);
            }
            return obj
                    .slice(0,4)
                    .concat({ id: 77, text: '开始抽奖' })
                    .concat(obj.slice(4));
        },

        Swap( obj, begin, end) {
            let temp = obj[begin];
            obj[begin] = obj[end];
            obj[end] = temp;
        },

        bindEvent() {
            $('.drawBox').on( 'click', e => {
                Child.run( $('.drawBox'), e.target.id );
            })
        }

    }
}